package org.gp953.user.service.impl;

import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.service.impl.BaseServiceImpl;
import org.gp953.basic.util.MD5Utils;
import org.gp953.basic.util.StrUtils;
import org.gp953.user.domain.LoginInfo;
import org.gp953.user.domain.User;
import org.gp953.user.dto.UserDto;
import org.gp953.user.mapper.LoginInfoMapper;
import org.gp953.user.mapper.UserMapper;
import org.gp953.user.service.IUserService;
import org.gp953.user.verifyconst.VerifyConst;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired

    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private String phone = null;

    @Override
    public void phoneRegister(UserDto userDto) {
        phone = userDto.getPhone();
        // 验证字段是否为空
        if (phone == null
                || userDto.getVerifyCode() == null
                || userDto.getPassword() == null
                || userDto.getConfirmPassword() == null){
            throw new BusinessException("请输入完整信息！");
        }
        // 验证2次密码是否输入一致
        if (!userDto.getPassword().equals(userDto.getConfirmPassword())) {
            throw new BusinessException("密码不一致，请重新输入！");
        }
        // 验证手机号是否已经被注册
        User userTemp = userMapper.queryByPhone(phone);
        if (userTemp!=null){
            throw new BusinessException("手机号已注册，请直接登录，如果忘记密码请点击找回密码");
        }
        // 获取验证码查看验证码是否过期
        Object verifycodeAndTimestamp = redisTemplate.opsForValue().get(VerifyConst.BUSINESS_REGISTER_PREFIX + phone);

        if (verifycodeAndTimestamp == null) { // 过期了就报错让用户重新获取
            throw  new BusinessException("验证码已过期，请重新获取");
        }
        // 没过期就验证验证码是否一致
        String verifycode = verifycodeAndTimestamp.toString().split(":")[0];
        if (!verifycode.equalsIgnoreCase(userDto.getVerifyCode())) {
            throw new BusinessException("验证码错误，请重新输入");
        }

        // 保存数据
        User user = UserDto2User(userDto);
        LoginInfo loginInfo = User2LoginInfo(user);
        loginInfoMapper.save(loginInfo);
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);


    }

    private LoginInfo User2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo);
//        loginInfo.setType(0);// 默认为0，代表是用户的登录信息不设置了
        return loginInfo;
    }

    private User UserDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        // 获取加密盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        user.setPassword(MD5Utils.encrypByMd5(userDto.getPassword() + salt));
        return user;
    }

}
